package com.example.fresh_fruit.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author :飞天の鱼
 * @date : 2022/8/18 16:53
 */
@Slf4j
@RestController
//该注解是@Controller和@ResponseBody的结合体，将响应数据直接塞到响应体里面

public class FileUpController {
    @PostMapping("/file1")
    public String modiFile(@RequestPart("file") MultipartFile headerImg) throws IOException {
        if (!headerImg.isEmpty()) {
            String originalFilename = headerImg.getOriginalFilename();
            String[] s=originalFilename.split("\\.");
            originalFilename= UUID.randomUUID().toString()+"."+s[s.length-1];
            headerImg.transferTo(new File("E:\\image\\" + originalFilename));
            return originalFilename;
        }
        return "null";
    }
}
